<?php

namespace think\wework\Api;

use think\wework\Abstracts\WeworkApiWithCache;

class Token extends WeworkApiWithCache
{
    private static ?Token $instance = null;
    public static function getInstance(): Token
    {
        if(!self::$instance instanceof Token) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * @return string
     */
    protected function getCacheKey(): string
    {
        $unique = md5($this->corpSecret);

        return md5('wework.api.token.' . $unique);
    }

    /**
     * @return string
     */
    protected function getFromServer(): string
    {
        $query = ['corpid' => $this->corpId, 'corpsecret' => $this->corpSecret];
        $data = $this->httpClient->get('gettoken', compact('query'))->toArray();

        return $data['access_token'];
    }
}
